package com.dongjiantong.farmrecords.model;
/**
 * 免疫记录子表
 */
import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Data
@Table(name = "farmrecords_immunization_sub")
public class FarmRecordsImmunizationSub extends BaseEntity {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = -8878318488897711672L;

    /**
     * 关联免疫记录表id
     */
    private Long vaccine_id;
    /**
     * 栏舍号id
     */
    private Long stable_id;
    /**
     * 栏舍号
     */
    private String stable_number;
    /**
     * 养殖区域
     */
    private Integer area;
    /**
     * 记录时间
     */
    private Date date;
    /**
     * 免疫类型
     */
    private Integer type;
    /**
     * 其他免疫类型
     */
    private String other_type;
    /**
     * 疫苗名称code
     */
    private Integer vaccine_code;
    /**
     * 疫苗名称name
     */
    private String vaccine_name;
    /**
     * 免疫天数
     */
    private Integer days;
    /**
     * 免疫次数
     */
    private Integer times;
    /**
     * 免疫数量
     */
    private Integer quantity;
    /**
     * 免疫用药量
     */
    private Float drug_quantity;
    /**
     * 免疫人员名称
     */
    private String operator_name;
    /**
     * 免疫方式
     */
    private Integer method;
    /**
     * 免疫用药剂量单位
     */
    private Integer drug_unit;
    /**
     * 单据是否有效
     */
    private Integer delete_flag;
    /**
     * 厂商，数组，照片url的数组
     */
    @JsonIgnore
    private String manufacturer;
    @Transient
    private List<String> manufacturerList;

    /**
     * 批号，数组
     */
    @JsonIgnore
    private String batch;
    @Transient
    private List<String> batchList;

    @JsonIgnore
    private String cow_photo_json;
    @Transient
    private List<String> cowPhoto;

    /**
     * 关联免疫记录主表 (一对一)
     */
    @Transient
    private FarmRecordsImmunization farmRecordsImmunization;

    public void setBatch(String batch) {
        this.batch = batch;
        if (Strings.isNullOrEmpty(batch)) {
            this.batchList = null;
        } else {
            try {
                this.batchList = objectMapper.readValue(batch,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setBatchList (List<String> batchList) {
        this.batchList = batchList;
        if (batchList == null && batchList.isEmpty()) {
            this.batch = "[]";
        } else {
            try {
                this.batch = objectMapper.writeValueAsString(batchList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setManufacturer (String manufacturer) {
        this.manufacturer = manufacturer;
        if (Strings.isNullOrEmpty(manufacturer)) {
            this.manufacturerList = null;
        } else {
            try {
                this.manufacturerList = objectMapper.readValue(manufacturer,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setManufacturerList (List<String> manufacturerList) {
        this.manufacturerList = manufacturerList;
        if (manufacturerList == null && manufacturerList.isEmpty()) {
            this.manufacturer = "[]";
        } else {
            try {
                this.manufacturer = objectMapper.writeValueAsString(manufacturerList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setCow_photo_json(String cow_photo_json) {
        this.cow_photo_json = cow_photo_json;

        if (Strings.isNullOrEmpty(cow_photo_json)) {
            this.cowPhoto = null;
        } else {
            try {
                this.cowPhoto = objectMapper.readValue(cow_photo_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setCowPhoto(List<String> cowPhoto) {
        this.cowPhoto = cowPhoto;

        if (cowPhoto == null || cowPhoto.isEmpty()) {
            this.cow_photo_json = "[]";
        } else {
            try {
                this.cow_photo_json = objectMapper.writeValueAsString(cowPhoto);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}